package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;
import com.cz.nongyetongv5.R;

public class ArchiveMapActivity extends Activity implements View.OnClickListener {
    private RelativeLayout btnBack;
    private Double longitude;
    private Double latitude;
    private String zoom;

    private TextView tv_longitude;
    private TextView tv_latitude;
    private TextView tv_zoom;
    private BaiduMap mBaiduMap;

    private MapView mapView;
    private Marker marker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_archive_map);
        initView();
    }

    private void initView() {
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        tv_longitude=findViewById(R.id.tv_longitude);
        tv_latitude =findViewById(R.id.tv_latitude);
        tv_zoom=findViewById(R.id.tv_zoom);
        mapView=findViewById(R.id.mapView);
        longitude = getIntent().getDoubleExtra("longitude", 0);
        latitude = getIntent().getDoubleExtra("latitude", 0);
        zoom = getIntent().getStringExtra("zoom");
        tv_longitude.setText("经度:" + longitude);
        tv_latitude.setText("纬度:" + latitude);
        tv_zoom.setText("缩放:" + zoom);
        mBaiduMap = mapView.getMap();
        UiSettings settings = mBaiduMap.getUiSettings();
        settings.setAllGesturesEnabled(false);   //关闭一切手势操作
        settings.setScrollGesturesEnabled(false);
        settings.setOverlookingGesturesEnabled(false);//屏蔽双指下拉时变成3D地图
        settings.setRotateGesturesEnabled(false);//屏蔽旋转
        settings.setZoomGesturesEnabled(false);//获取是否允许缩放手势返回:是否允许缩放手势
        mapView.removeViewAt(2);
        LatLng point = new LatLng(latitude, longitude);
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.icon_location);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap).zIndex(9).draggable(true);
        //在地图上添加Marker，并显示
        marker = (Marker) (mBaiduMap.addOverlay(option));
        final MapStatus mMapStatus = new MapStatus.Builder()
                .target(point)
                .zoom(Float.parseFloat(zoom))
                .build();
        //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        //改变地图状态
        mBaiduMap.setMapStatus(mMapStatusUpdate);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnBack:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        if (mapView != null) {
            mapView.onDestroy();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }
}
